<?php

namespace app\admin\controller;

use app\common\model\ArticleModel;

class Article extends Base
{
	// 列表
    public function index()
    {
        if(request()->isAjax()) {
        	$page = input('get.page', 1, 'intval');
            $title = input('get.title');

            $where = [
                ['status', '=', 1],
            ];
            $title && $where[] = ['title', 'like', '%'.$title.'%'];

            $ArticleModel = new ArticleModel();
            $pageList = $ArticleModel->getPageList($where, $page, $limit);
            if ($pageList['total'] > 0) {
                $category_list = ArticleModel::$category_list;
                foreach ($pageList['data'] as $key => &$v) {
                    $v['create_date'] = formatDate($v['create_time']);
                    $v['category_zh'] = $category_list[$v['category']];
                }
            }

            return json(['code' => 0, 'msg' => '', 'count' => $pageList['total'], 'data' => $pageList['data']]);
        }

        return $this->fetch('index');
    }

    // 添加
    public function add()
    {
    	$request = request();
        if($request->isPost()) {
            $title = input('post.title', '', 'trim');
            $content = input('post.content', '', 'trim');
            $author = input('post.author', '', 'trim');
            $category = input('post.category', 0, 'intval');
            $is_publish = input('post.is_publish', -1, 'intval');
            $file_path = input('post.file_path', '', 'trim');
            $brief = input('post.brief', '', 'trim');

            if(mb_strlen($title, 'utf-8') > 255) {
                return json(['code' => -1, 'msg' => '文章标题太长了']);
            }

            $timestamp = time();
            $insertData = [
                'title' => $title,
            	'brief' => $brief,
            	'content' => $content,
            	'author' => $author,
            	'category' => $category,
            	'is_publish' => $is_publish,
            	'status' => 1,
            	'create_time' => $timestamp,
            	'update_time' => $timestamp,
            ];
            !empty($file_path) && $insertData['file_path'] = $file_path;
            // dump($insertData);die;
            $ArticleModel = new ArticleModel();
            $res = $ArticleModel->add($insertData);

            if ($res) {
            	return json(['code'=>0, 'msg'=>'文章添加成功']);
            }
            return json(['code'=>-1, 'msg'=>'文章添加失败']);
        }

        $category_list = ArticleModel::$category_list;
        $this->assign([
            'category_list' => $category_list,
            'action' => url('article/add'),
        ]);

        return $this->fetch('add');
    }

    // 编辑
    public function edit()
    {
        $id = input('param.id', 0, 'intval');

        $ArticleModel = new ArticleModel();
        $request = request();
        if($request->isPost()) {
            $title = input('post.title', '', 'trim');
            $content = input('post.content', '', 'trim');
            $author = input('post.author', '', 'trim');
            $is_publish = input('post.is_publish', -1, 'intval');
            $category = input('post.category', 0, 'intval');
            $file_path = input('post.file_path', '', 'trim');
            $brief = input('post.brief', '', 'trim');

            // dump($title);die;
            if(mb_strlen($title, 'utf-8') > 255) {
                return json(['code' => -1, 'msg' => '文章标题太长了']);
            }

            $timestamp = time();
            $updateData = [
                'title' => $title,
                'brief' => $brief,
                'content' => $content,
                'author' => $author,
                'category' => $category,
                'is_publish' => $is_publish,
                'update_time' => $timestamp,
            ];
            !empty($file_path) && $updateData['file_path'] = $file_path;
            // dump($insertData);die;
            $res = $ArticleModel->where(['id'=>$id])->update($updateData);

            if ($res) {
                return json(['code'=>0, 'msg'=>'文章编辑成功']);
            }
            return json(['code'=>-1, 'msg'=>'文章编辑失败']);
        }

        $oss_upload_config = config('LOCAL_CONFIG');
        $info = $ArticleModel->getInfo(['id'=>$id]);
        if (!empty($info['file_path'])) {
            $info['file_url'] = $oss_upload_config['url'] . $info['file_path'];
        }
        $category_list = ArticleModel::$category_list;
        $this->assign([
            'id' => $id,
            'info' => $info,
            'category_list' => $category_list,
            'action' => '/admin/article/edit/id/'.$id.'.html',
        ]);

        return $this->fetch('add');
    }

    //删除
    public function del()
    {
        $id = input('param.id', 0, 'intval');

        $ArticleModel = new ArticleModel();
        $info = $ArticleModel->getInfo(['id'=>$id]);
        if (empty($id) || empty($info)) {
           return json(['code'=>-1, 'msg'=>'文章不存在']);
        }

        $timestamp = time();
        $updateData = [
            'status' => -1,
            'update_time' => $timestamp,
        ];
        // dump($insertData);die;
        $res = $ArticleModel->where(['id'=>$id])->update($updateData);

        if ($res) {
            return json(['code'=>0, 'msg'=>'删除成功']);
        }
        return json(['code'=>-1, 'msg'=>'删除失败']);
    }
}